/*
 * ZoombieView.cpp
 *
 *  Created on: 31/05/2014
 *      Author: javier
 */

#include "ZoombieView.h"

ZoombieView::ZoombieView(SDLWindow* window, SDL_Texture* texture,
		Positionable* object, ZoombieColor color) :
		cycler(SPRITES_TAM / 4) {
	this->window = window;
	this->texture = texture;
	this->object = object;
	position.first = object->getX();
	position.second = object->getY();
	previousPosition.first = position.first;
	previousPosition.second = position.second;
	base = 0;
	int yBase;
	if (color == Z_GREEN) {
		yBase = 0;
	} else if (color == Z_BLUE) {
		yBase = 50;
	} else if (color == Z_RED) {
		yBase = 100;
	}

	for (int i = 0; i < SPRITES_TAM; ++i) {
		clips[i].x = i * 30;
		clips[i].y = yBase;
		clips[i].w = 30;
		clips[i].h = 50;
	}
	useClip = 0;
}

void ZoombieView::render(Camera& cam) {
	SDL_Rect render = cam.getOffsetFromModel(position);
	render.w *= 0.75;
	window->renderTexture(texture, &render,
			&clips[base * (SPRITES_TAM / 4) + useClip]);

}

void ZoombieView::update() {
	position.first = previousPosition.first
			+ (object->getX() - previousPosition.first) * 0.5f;
	position.second = previousPosition.second
			+ (object->getY() - previousPosition.second) * 0.5f;
	if (Utils::absoluteValue(object->getX() - previousPosition.first) < 0.1
			&& Utils::absoluteValue(object->getY() - previousPosition.second)
					< 0.1) {
		previousPosition.first = position.first = object->getX();
		previousPosition.second = position.second = object->getY();
		return;
	}
	if (position.first < previousPosition.first) {
		base = 3;
		useClip = cycler.next();
	} else if (position.first > previousPosition.first) {
		base = 1;
		useClip = cycler.next();
	} else if (position.second > previousPosition.second) {
		base = 0;
		useClip = cycler.next();
	} else if (position.second < previousPosition.second) {
		base = 2;
		useClip = cycler.next();
	}
	useClip = cycler.next();
	previousPosition.first = position.first;
	previousPosition.second = position.second;
}

ZoombieView::~ZoombieView() {
// TODO Auto-generated destructor stub
}

